package com.css.openapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.css.common.core.exception.ServiceException;
import com.css.common.core.session.SessionUtils;
import com.css.common.core.utils.GyUtils;
import com.css.openapi.BO.*;
import com.css.openapi.entity.ZfjkpFxkpTssq;
import com.css.openapi.entity.ZfjkpFxkpTssqmxb;
import com.css.openapi.feign.ZfjkpPtCommonClient;
import com.css.openapi.mapper.ZfjkpFxkpTssqmxbMapper;
import com.css.openapi.service.ZfjkpFxkpTssqService;
import com.css.openapi.mapper.ZfjkpFxkpTssqMapper;
import com.css.openapi.service.ZfjkpFxkpTssqmxbService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author 21062
* @description 针对表【zfjkp_fxkp_tssq(退税申请表)】的数据库操作Service实现
* @createDate 2025-10-10 16:58:29
*/
@Service
public class ZfjkpFxkpTssqServiceImpl extends ServiceImpl<ZfjkpFxkpTssqMapper, ZfjkpFxkpTssq>
    implements ZfjkpFxkpTssqService{

    @Resource
    private ZfjkpPtCommonClient zfjkpPtCommonClient;

    @Resource
    private ZfjkpFxkpTssqmxbService tssqmxbService;

    @Override
    @Transactional
    public TssqBO refundTaxCommit(TssqParamsBO params) {
        TssqResponse response = zfjkpPtCommonClient.refundTaxCommit(params);
        if(!Objects.equals(0,response.getCode()))throw new ServiceException(response.getMsg());
        ZfjkpFxkpTssq tssq = new ZfjkpFxkpTssq();
        BeanUtil.copyProperties(response, tssq);
        tssq.setUuid(IdUtil.fastSimpleUUID());
        tssq.setCjr(SessionUtils.getCjr());
        tssq.setCjsj(new Date());
        this.save(tssq);
        if (GyUtils.isNotNull(response.getYtskmxList())){
            List<ZfjkpFxkpTssqmxb> tssqmxbList = new ArrayList<>();
            for (YtskmxListBO ytskmxListBO : response.getYtskmxList()) {
                ZfjkpFxkpTssqmxb tssqmxb = new ZfjkpFxkpTssqmxb();
                BeanUtil.copyProperties(ytskmxListBO, tssqmxb);
                tssqmxb.setUuid(IdUtil.fastSimpleUUID());
                tssqmxb.setTsid(tssq.getUuid());
                tssqmxb.setCjr(SessionUtils.getCjr());
                tssqmxb.setCjsj(new Date());
                tssqmxbList.add(tssqmxb);
            }
            tssqmxbService.saveBatch(tssqmxbList);
        }
        TssqBO tssqBO = new TssqBO();
        BeanUtil.copyProperties(response,tssqBO);
        List<YtskmxListBO> ytskmxList = new ArrayList<>(response.getYtskmxList());
        tssqBO.setYtskmxList(ytskmxList);
        return tssqBO;
    }

    @Override
    @Transactional
    public TsjgBO refundTaxResult(TsjgParamsBO params) {
        TsjgResponse response = zfjkpPtCommonClient.refundTaxResult(params);
        if(!Objects.equals(0,response.getCode()))throw new ServiceException(response.getMsg());
        ZfjkpFxkpTssq tssq = this.lambdaQuery()
                .eq(ZfjkpFxkpTssq::getZrrxm,params.getZrrxm())
                .eq(ZfjkpFxkpTssq::getZrrsfzjhm,params.getZrrsfzjhm())
                .in(GyUtils.isNotNull(params.getTdssqxhList()),ZfjkpFxkpTssq::getTdssqxh,params.getTdssqxhList())
                .one();
        tssq.setSkssswjgDm(response.getSksssqjgDm());
        tssq.setSkssswjgMc(response.getSkssswjgMc());
        tssq.setSqjg(response.getTssqzt());
        tssq.setSpbtgyy(response.getSpbtyyy());
        tssq.setXgr(SessionUtils.getCjr());
        tssq.setXgsj(new Date());
        this.updateById(tssq);
        TsjgBO tsjgBO = new TsjgBO();
        BeanUtil.copyProperties(response,tsjgBO);
        return tsjgBO;
    }
}




